package com.heatup.apt.management.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;
import com.heatup.apt.common.dao.user.UserAccountRechargeMapper;
import com.heatup.apt.management.service.custom.UserAccountRechargeService;
import com.heatup.apt.model.user.UserAccountRecharge;
import com.heatup.apt.model.user.UserInfo;

@Service
public class UserAccountRechargeServiceImpl extends  SuperServiceImpl<UserAccountRechargeMapper,UserAccountRecharge> implements UserAccountRechargeService {
	
	@Autowired
	private UserAccountRechargeMapper userAccountRechargeMapper;
	
	/**
	 * 充值记录分页查询
	 */
	@Override
	public Page<UserAccountRecharge> queryUserAccountConsumePage(Page<UserAccountRecharge> userAccountRechargePage,
			Map<String, Object> paramsMap) {
		List<UserAccountRecharge> list = userAccountRechargeMapper.queryUserAccountRechargePage(userAccountRechargePage, paramsMap);
		userAccountRechargePage.setRecords(list);
		return userAccountRechargePage;
	}
	
	/**
	 * 查询充值记录 类型 
	 */
	@Override
	public Map<String, Object> queryUserAccountRechargePageAll(Map<String, Object> map) {
		Map<String, Object> responsemap = new HashMap<String, Object>();
		int pageCount = 10 ;
		int pageSum = 0;
		int pageNo = (Integer)map.get("pageNo");
		map.put("pageNo",pageNo);
		map.put("page","page");
		if( pageNo != 1){
			map.put("pageNum", pageNo);
			pageNo = pageCount * (pageNo-1);
			map.put("pageNo", pageNo);
			map.put("pageCount", pageCount);
		}else{
			map.put("pageNo", 0);
			map.put("pageNum", 1);
			map.put("pageCount", pageCount);
		}
		// 时间分割
		String payTimes = (String) map.get("pay_time"); 
		String payTimeArray[] = payTimes.split("~");
		if(payTimes.length() > 0 && payTimes != null){
			map.put("startTime", payTimeArray[0]);
			map.put("endTime", payTimeArray[1]);
		}
		// 类型
		Integer vipType = (Integer) map.get("vipType");
		if(vipType == 0){
			map.put("accountType", null);
		}else if(vipType == 1){
			map.put("accountType", "新增");
		}else if(vipType == 2){
			map.put("accountType", "续费");
		}
		List<Map<String,Object>> listAll = new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		list = userAccountRechargeMapper.quertUserAccountRechargePageAll(map);
		map.remove("page");
		listAll = userAccountRechargeMapper.quertUserAccountRechargePageAll(map);
		BigDecimal allXF = new BigDecimal(0);
		BigDecimal allSF = new BigDecimal(0);
		for(Map m : listAll){
			allXF = allXF.add((BigDecimal)m.get("face_amt"));
			allSF = allSF.add((BigDecimal)m.get("actual_amt"));
		}
		Integer userId = (Integer) map.get("userId");
		if(userId == 0){
			map.remove("userId");
		}
		map.put("list",list);
		map.put("pageCount",listAll.size());
		map.put("allXF", allXF);
		map.put("allSF", allSF);
		return map;
	}
	
	/**
	 * 计算可退款金额
	 * @param parmeMap
	 * @return
	 */
	public Map<String,Object> calculationRdfundAmt(Map<String,Object> parmeMap){
		Map<String,Object> map = new HashMap<String,Object>();
		UserAccountRecharge userAccountRecharge = null;
		UserInfo userInfo = (UserInfo) parmeMap.get("userInfo");
		BigDecimal userAmt = userInfo.getAccountAmt();
		BigDecimal refundAmt = BigDecimal.valueOf(0);
		int num = 0 ;
		Integer pageNo = 0 ;
		try {
			// 先判断用户账户余额是否等0 
			if(userAmt.compareTo(BigDecimal.ZERO) != 0){
				// 递归判断用户可退余额
				// 原理：先判断y1(用户余额)是否满足退款，满足 查出c1(最后一笔充值记录)判断 用户余额是y否大于z1(赠送金额 )如果大于 
				// 用当前y2(y=y-c1)余额减去 最后一笔实到金额,不大于的时候 退款金额为 0 ，再次判断y是否大于0,不大于时 退款金额为 t=y1-z1,大于时 判断y2是否大于z2
				// t=t+(y2-z2)
				while(userAmt.compareTo(BigDecimal.ZERO) > 0){
					userAccountRecharge = userAccountRechargeMapper.queryUserAccountRechargeLast(userInfo.getId(),pageNo);
					if(userAccountRecharge == null){
						break;
					}
					// 表示的是微信充值的
					if(userAccountRecharge.getRechargeType() == 1){
						BigDecimal faceAmt = userAccountRecharge.getFaceAmt();// 实付金额
						// face_amt 实付金额  actual_amt 实到金额 
						// 1.判断 当前账户余额是否大于 赠送金额
						// 1.1 大于
						if(userAmt.compareTo(userAccountRecharge.getActualAmt().subtract(userAccountRecharge.getFaceAmt())) > 0){
							// 判断 当前余额是否大于 实到金额
							if(userAmt.compareTo(userAccountRecharge.getActualAmt()) >= 0){
								refundAmt = refundAmt.add(userAccountRecharge.getFaceAmt());
								userAmt = userAmt.subtract(userAccountRecharge.getActualAmt());
								pageNo += 1;
								num += 1;
							}else{
							// 当前余额不大于 实到金额 只是大于赠送金额
								refundAmt = refundAmt.add(userAmt.subtract(userAccountRecharge.getActualAmt().subtract(userAccountRecharge.getFaceAmt())));
								userAmt = BigDecimal.valueOf(0);
								num += 1;
								map.put("num", num);
								map.put("refunAmt", refundAmt);
								break;
							}
						}else{
						// 1.2
							//userAmt = BigDecimal.valueOf(0);
							//refundAmt = BigDecimal.valueOf(0);
							map.put("num", num);
							map.put("refunAmt", refundAmt);
							break;
						}
					}else{// 表示是兑换码充值的
						// 如果是兑换码充值的话那么就是退款金额不变
						userAmt = userAmt.subtract(userAccountRecharge.getFaceAmt());
						pageNo += 1;
					}
					
				}
				map.put("num", num);
				map.put("refunAmt", refundAmt);
			}else{
				map.put("num", 0);
				map.put("refunAmt", 0);
			}	
			
		} catch (Exception e) {
			// TODO: handle exception
			logger.info("======================== calculationRdfundAmt error:运行异常"+e.getMessage());
		}
		return map;
	}

}